package com.semlab.client.mvp.home;

import java.util.List;
import java.util.logging.Logger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.*;
import com.semlab.client.bundle.Resources;
import com.semlab.client.events.ClientLoginEvent;
import com.semlab.client.fwk.Event;
import com.semlab.client.fwk.ap.mvp.BaseView;
import com.semlab.client.mvp.home.widgets.ImportPanel;
import com.semlab.client.mvp.home.widgets.MoviesExplorer;
import com.semlab.client.proxy.ClientProxy;
import com.semlab.client.proxy.MoviePageProxy;
import com.semlab.client.proxy.MovieProxy;
import com.semlab.client.widgets.UICallback;


public class HomeView extends BaseView<HomeMVP.Presenter> implements HomeMVP.View, RequiresResize, ProvidesResize {

    private static final Logger log = Logger.getLogger(HomeView.class.getName());

    private static NavigatordViewUiBinder uiBinder = GWT.create(NavigatordViewUiBinder.class);


    interface NavigatordViewUiBinder extends UiBinder<Widget, HomeView> {
    }

    @UiField
    DockLayoutPanel container;

    @UiField
    MoviesExplorer moviesExplorer;
    @UiField
    DeckLayoutPanel deckPanel;
    @UiField
    Label clientData;


    public HomeView() {
        initWidget(uiBinder.createAndBindUi(this));
        deckPanel.showWidget(0);
        deckPanel.setAnimationDuration(800);
    }

    @Override
    public void onPresenterStart() {
        presenter.handleEvent(ClientLoginEvent.TYPE, new Event.Handler<ClientLoginEvent>() {
            @Override
            public void on(final ClientLoginEvent loginEvent) {
                clientData.setText(loginEvent.getClient().getFirstName() + " " + loginEvent.getClient().getLastName());
                clientData.addClickHandler(new ClickHandler() {
                    @java.lang.Override
                    public void onClick(ClickEvent event) {
                        Window.open("http://www.facebook.com/" + loginEvent.getClient().getFacebookId(), "_blank", "");
                    }
                });
                clientData.addStyleName(Resources.INSTANCE.css().hand());
            }
        });
    }

    @UiFactory
    MoviesExplorer makeMovieExplorer() { // method name is insignificant
        return new MoviesExplorer() {

            @Override
            public void getFriendsForAMovie(MovieProxy proxy, UICallback<List<ClientProxy>> friendsCallback) {
                presenter.getFriendsForAMovie(proxy, friendsCallback);
            }

            @Override
            public void getMovies(UICallback<MoviePageProxy> callback, int offset, int num) {
                presenter.getAllMovies(callback, offset, num);
            }

            @Override
            public void enrichMovieData(MovieProxy movie, UICallback<MovieProxy> detailsCallback) {
                presenter.enrichMovieData(movie, detailsCallback);
            }

            @Override
            public void getDetailsForAMovie(MovieProxy proxy, UICallback<MovieProxy> callback) {
                presenter.getDetailsForAMovie(proxy, callback);
            }
        };
    }

    @UiFactory
    ImportPanel makeImportPanel() {
        return new ImportPanel() {
            @Override
            public void startImport(int friends, int movies) {
                presenter.startImport(friends, movies);
            }
        };
    }

    @Override
    public void refreshMoviesExplorer() {
        moviesExplorer.refresh();
    }

    @java.lang.Override
    public void showMoviesExplorer() {
        deckPanel.showWidget(1);
    }
}
